/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import pojo.LoaiGiayPOJO;
import pojo.MauSacPOJO;
import util.MySQLDataAccessHelper;

/**
 *
 * @author NhatHuy
 */
public class MauSacDAO {
    /**
     * Phương thức lấy danh mục màu sắc.
     */
    public static ArrayList<MauSacPOJO> layDanhMucMauSac(){
        ArrayList<MauSacPOJO> ds = new ArrayList<MauSacPOJO>();
        try{
            //Kết nối csdl.
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            String sql = "SELECT * FROM MAUSAC";
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()){
                MauSacPOJO ms = new MauSacPOJO();
                ms.setMaMauSac(rs.getInt("MaMauSac"));
                ms.setTenMauSac(rs.getString("TenMauSac"));
                ds.add(ms);
            }
            //Đóng kết nối.
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return ds;
    }
    
    /**
     * Phương thức tìm mã màu sắc theo tên màu sắc.
     */
    public static int timMaMauSacTheoTenMau(String tenMauSac){
        int maMauSac = 0;
        try{
            //Kết nối csdl.
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            String sql = "SELECT MaMauSac FROM MAUSAC WHERE TenMauSac like '%"+tenMauSac+"%'";
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()){
                maMauSac = rs.getInt("MaMauSac");
            }
            //Đóng kết nối.
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return maMauSac;
    }
    
    /**
     * Phương thức tìm tên màu sắc theo mã màu sắc.
     */
    public static String timTenMauSacTheoMa(int maMauSac){
        String tenMauSac = "";
        try{
            //Kết nối csdl.
            MySQLDataAccessHelper helper = new MySQLDataAccessHelper();
            helper.open();
            String sql = "SELECT TenMauSac FROM MAUSAC WHERE maMauSac = "+maMauSac;
            ResultSet rs = helper.executeQuery(sql);
            while (rs.next()){
                tenMauSac = rs.getString("TenMauSac");
            }
            //Đóng kết nối.
            helper.close();
        }catch (Exception ex){
            System.out.println(ex.getMessage());
        }
        return tenMauSac;
    }
}
